package com.jiangnanxiaoguan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jiangnanxiaoguan.entity.Category;
import com.jiangnanxiaoguan.entity.Dish;
import com.jiangnanxiaoguan.entity.Setmeal;
import com.jiangnanxiaoguan.exception.BusinessException;
import com.jiangnanxiaoguan.mapper.CategoryMapper;
import com.jiangnanxiaoguan.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangnanxiaoguan.service.DishService;
import com.jiangnanxiaoguan.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author 不愿透露姓名的狂热者
 * @since 2022-09-11
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    protected SetmealService setmealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        if (dishService.count(dishLambdaQueryWrapper) > 0) {
            throw new BusinessException("该分类已有关联菜品，删除失败！");
        }

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        if (setmealService.count(setmealLambdaQueryWrapper) > 0) {
            throw new BusinessException("该分类已有关联套餐，删除失败！");
        }

        super.removeById(id);
    }
}
